//
//  MessageCell.m
//  JinMaJiaDemo
//
//  Created by GripMeMory on 16/1/19.
//  Copyright © 2016年 GripMeMory. All rights reserved.
//

#import "MessageCell.h"

#define kSeparateMargin 20 / 2
#define kTitleLabelLeftMargin 60 / 2
#define kTitleLabelTopMargin 20 / 2
#define kTitleLabelWidth 150 / 2
#define kTitleLabelHeight 30 / 2
#define kTimeLabelRightMargin 30 / 2
#define kTimeLabelWidth 250 / 2
#define kBottomLabelHeight 54 / 2
#define kImageViewWidth 20 / 2
#define kImageViewTopMargin 15 / 2
#define kTimeViewRightMargin 10 / 2


@implementation MessageCell

{
    UIView *_backGroundView;
    UIView *_separateView;
    UILabel *_titleLabel;
    UILabel *_timeLabel;
    UILabel *_contentLabel;
    UILabel *_noticeLabel;
    UIImageView *_imageView;
    UIImageView *_timeView;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _backGroundView = [[UIView alloc] init];
        _backGroundView.backgroundColor = [UIColor whiteColor];
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.backgroundColor = [UIColor whiteColor];
        _titleLabel.adjustsFontSizeToFitWidth = YES;
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.textAlignment = NSTextAlignmentRight;
        _timeLabel.font = [UIFont systemFontOfSize:12];
        _timeLabel.textColor = RGB_COLOR(@"#c9c9c9");
        
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.numberOfLines = 4;
        _contentLabel.font = [UIFont systemFontOfSize:12];
        
        _noticeLabel = [[UILabel alloc] init];
        _noticeLabel.font = [UIFont systemFontOfSize:12];
        
        _separateView = [[UIView alloc] init];
        _separateView.backgroundColor = RGB_COLOR(@"#c9c9c9");
        
        _imageView = [[UIImageView alloc] init];
        _imageView.image = [UIImage imageNamed:@"进入"];
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
        
        _timeView = [[UIImageView alloc] init];
        _timeView.image = [UIImage imageNamed:@"时钟"];
        _timeView.contentMode = UIViewContentModeScaleAspectFit;
        
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor clearColor];
        [_backGroundView addSubview:_titleLabel];
        [_backGroundView addSubview:_timeLabel];
        [_backGroundView addSubview:_noticeLabel];
        [_backGroundView addSubview:_contentLabel];
        [_backGroundView addSubview:_separateView];
        [_backGroundView addSubview:_imageView];
        [_backGroundView addSubview:_timeView];
        [self addSubview:_backGroundView];
    }
    return self;
}

- (void)layoutSubviews{
    _backGroundView.frame = CGRectMake(0, kSeparateMargin, self.width, self.height - kSeparateMargin);
    _titleLabel.frame = CGRectMake(kTitleLabelLeftMargin, kTitleLabelTopMargin, kTitleLabelWidth, kTitleLabelHeight);
    _timeLabel.frame = CGRectMake(self.right - kTimeLabelRightMargin - kTimeLabelRightMargin, _titleLabel.top, kTimeLabelWidth, kTitleLabelHeight);
    _contentLabel.frame = CGRectMake(_titleLabel.left, _titleLabel.bottom, UISCREENWIDTH - kTitleLabelLeftMargin - kTimeLabelRightMargin, _backGroundView.height - 2 * kTitleLabelTopMargin - kBottomLabelHeight - kSeparateMargin);
    _separateView.frame = CGRectMake(kTitleLabelLeftMargin, _contentLabel.bottom, _contentLabel.width, 1);
    _noticeLabel.frame = CGRectMake(_titleLabel.left, _contentLabel.bottom, UISCREENWIDTH - kTitleLabelLeftMargin - kTimeLabelRightMargin, kBottomLabelHeight);
    _imageView.frame = CGRectMake(self.right - kTimeLabelRightMargin - kImageViewWidth, _contentLabel.bottom + kImageViewTopMargin, kImageViewWidth, _noticeLabel.height - 2 *kImageViewTopMargin);
    CGRect rect = _timeLabel.frame;
    CGRect rect2 = [_timeLabel textRectForBounds:rect limitedToNumberOfLines:1];
    _timeLabel.frame = CGRectMake(self.right - kTimeLabelRightMargin - rect2.size.width, rect.origin.y, rect2.size.width, rect.size.height);
    _timeView.frame = CGRectMake(_timeLabel.left - kTitleLabelHeight - kTimeViewRightMargin, _timeLabel.top, kTitleLabelHeight, kTitleLabelHeight);
}

- (void)configureCellWithModel:(MessageModel *)model{
    _titleLabel.text = model.title;
    _contentLabel.text = model.mainMessage;
    _noticeLabel.text = @"查看全文";
    _timeLabel.text = model.time;
}

@end
